package com.longrise.android.byjk.plugins.vip.dailysign;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;

/**
 * Created by callxkj on 2017/4/14.
 * 每日签到
 */

public class DailySignPresenter extends DailySignContract.Presenter {
    @Override
    protected void init() {

    }

    @Override
    void refreshData(boolean isNeedSign) {
        toSign();
    }

    /**
     * 签到
     */
    private void toSign() {
        UserInfor userInfor = UserInfor.getInstance();
        EntityBean bean = new EntityBean();
        bean.set("userid", userInfor.getUserid());
        bean.set("cardno", userInfor.getUsersfzh());
        bean.set("nickname", userInfor.getUserNickName());
        bean.set("userheadurl", userInfor.getUserHeadIcon());
        bean.set("companyname", userInfor.getCompanyname());
        bean.set("managerzonename", userInfor.getManagerzonename());
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        mView.showLoadingDialog();
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_vip_iVipUserScoresbyqd", fbean, new LoadDataManager.OnRequestCallListener() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            parseSignBean(bean);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {

                    }
                });
    }

    /**
     * 解析签到的数据
     */
    private void parseSignBean(EntityBean bean) {
        String resultdesc = bean.getString("resultdesc");
        int resultstate = (Integer) bean.get("resultstate");
        if (resultstate == 1) {
            EntityBean resultbean = bean.getBean("result");
            String continuitydays = resultbean.getString("continuitydays");
            String acquiredscore = resultbean.getString("acquiredscore");
            String infostate = resultbean.getString("infostate"); //2 当日已经签过到了  1 当日签到成功   0当日未签到
            String totalscores = resultbean.getString("totalscores");
            switch (infostate) {
                case "0": //当日未签到  签到失败
                    mView.showToast(resultdesc);
                    break;
                case "1": //当日签到成功
                    mView.showSignSuccessDialog(continuitydays, acquiredscore, totalscores);
                    break;
                case "2": //当日已经签过到了
                    break;
            }

        } else {
            mView.showToast(resultdesc);
        }
        toGetSignScores();
        toGetSignScoreRank();
    }

    /**
     * 会员当日签到 7天应获得积分
     */
    private void toGetSignScores() {
        EntityBean bean = new EntityBean();
        bean.set("userid", UserInfor.getInstance().getUserid());
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_vip_sVipUserScoresbyqd", fbean, new LoadDataManager.OnRequestCallListener() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            parseSignScores(bean);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {

                    }
                });
    }

    private void parseSignScores(EntityBean bean) {
        String resultdesc = bean.getString("resultdesc");
        int resultstate = (Integer) bean.get("resultstate");
        if (resultstate == 1) {
            EntityBean resultbean = bean.getBean("result");
            String continuitydays = resultbean.getString("continuitydays");
            String vipscores = resultbean.getString("vipscores");
            EntityBean[] qdInfoses = resultbean.getBeans("qdInfos");
            mView.showUserScores(continuitydays,vipscores,qdInfoses);
        } else {
            mView.showToast(resultdesc);
        }
    }

    /**
     * 会员签到光荣榜 查询
     */
    private void toGetSignScoreRank() {
        EntityBean bean = new EntityBean();
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_vip_sVipUserScoresrank", fbean, new LoadDataManager.OnRequestCallListener() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            String resultdesc = bean.getString("resultdesc");
                            int resultstate = (Integer) bean.get("resultstate");
                            if (resultstate == 1) {
                                EntityBean[] resultbeans = bean.getBeans("result");
                                mView.showScoresrank(resultbeans);
                            } else {
                                mView.showToast(resultdesc);

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {

                    }
                });
    }
}
